// pages/course/evaluate/index.js
var request = require("../../../utils/request.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pickers:{
      0: true,
      1: true,
      2: true,
      3: true,
      4: true
    },
    pickerTips:"非常棒",
    star:5,
    CourseID:0,
    CustomerID:0,
    Content:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var CourseID = options.CourseID;
    console.log(CourseID);
    var customId = app.globalData.userInfo['ID'];
    console.log("customId:" + customId);
    this.setData({
      CourseID: CourseID,
      CustomerID: customId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  //课程评价
  pickerStar:function(e){
    var index = e.currentTarget.dataset.index;
    var pickerTips = "";
    switch(parseInt(index)){
      case 0:
        pickerTips = "非常差";
        break;
      case 1:
        pickerTips = "差";
        break;
      case 2:
        pickerTips = "中等";
        break;
      case 3:
        pickerTips = "棒";
        break;
      case 4:
        pickerTips = "非常棒";
        break;
    }

    console.log(index);
    var pickers = {};
    for(let i = 0; i < 5;i++){
      if(i <= index){
        pickers[i] = true;
      }
      else{
        pickers[i] = false;
      }
    };
    console.log(pickers);
    this.setData({
      pickers:pickers,
      pickerTips:pickerTips,
      star:index
    })
  },

  getContent:function(e){
    var value = e.detail.value;
    console.log(value);
    this.setData({
      Content:value
    })
  },

  // submit：提交评论
  submit:function(e){
    var that = this;
    var StarLevel = that.data.star;
    var Content = that.data.Content;
    var CourseID = that.data.CourseID;
    var CustomerID = that.data.CustomerID;

    if (Content.trim() == ''){
      wx.showToast({
        title: '评价内容不能为空',
      })

      return false;
    }

    var data = {
      CourseID: parseInt(CourseID),
      CustomerID: parseInt(CustomerID),
      StarLevel: parseInt(StarLevel),
      Content: Content
    }
    
    console.log(data);

    request.request("api/Course/AddCourseComment",data,"POST").then(function(res){
      console.log(res);
      if(res.data.status == 1){
        wx.showToast({
          title: '评价成功',
        })
      }
    })
  }
})